package com.cloud.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.crypto.symmetric.AES;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据加密工具
 *
 * @author yzj
 */
public class DataSignUtils {

    /**
     * 获取AES/对称加密KEY
     * 加密模式:ECB, 填充方式:pkcs5padding, 字符集:UTF-8, 输入格式:string, 输出格式:hex
     *
     * @return
     */
    public static String getAesKet() {
        return IdUtil.simpleUUID();
    }

    /**
     * AES/对称加密
     *
     * @param desKet
     * @param data   数据
     * @return
     */
    public static String aesEncrypt(String desKet, String data) {
        try {
            AES aes = SecureUtil.aes(desKet.getBytes());
            // 加密为16进制表示
            return aes.encryptHex(data);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * AES/对称解密
     *
     * @param desKet
     * @param data   密文
     * @return
     */
    public static String aesPuzzle(String desKet, String data) {
        try {
            AES aes = SecureUtil.aes(desKet.getBytes());
            return aes.decryptStr(data, CharsetUtil.CHARSET_UTF_8);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取RSA/非对称加密KEY
     *
     * @return
     */
    public static Map<String, String> getRsaKet() {
        RSA rsa = new RSA("RSA");
        Map<String, String> mapResult = new HashMap<>();
        // 获取私钥
        mapResult.put("privateKey", rsa.getPrivateKeyBase64());
        // 获取公钥
        mapResult.put("publicKey", rsa.getPublicKeyBase64());
        return mapResult;
    }

    /**
     * RSA/非对称加密
     *
     * @param privateKey
     * @param publicKey
     * @param data
     * @return
     */
    public static String rsaEncrypt(String privateKey, String publicKey, String data) {
        try {
            RSA rsa = new RSA("RSA", privateKey, publicKey);
            byte[] encrypt = rsa.encrypt(data.getBytes(), KeyType.PublicKey);
            return new String(encrypt);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * RSA/非对称解密
     *
     * @param privateKey
     * @param publicKey
     * @param data
     * @return
     */
    public static String rsaPuzzle(String privateKey, String publicKey, String data) {
        try {
            RSA rsa = new RSA("RSA", privateKey, publicKey);
            return new String(rsa.decrypt(data.getBytes(), KeyType.PrivateKey));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 数据签名
     *
     * @param data
     * @return
     */
    public static String setDataSign(String data) {
        try {
            Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
            // 数据签名
            return new String(sign.sign(data));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 数据签名检验
     *
     * @param data
     * @return
     */
    public static boolean checkDataSign(String data, String signData) {
        try {
            Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
            // 数据签名
            return sign.verify(data.getBytes(), signData.getBytes());
        } catch (Exception e) {
            return false;
        }
    }

}
